using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

public class ResoucesLoader : MonoBehaviour
{
	public Object[] objectsToLoad;

	public Camera loadCamera;

	public SpriteRenderer splashGreyLeft;

	public SpriteRenderer splashGreyRight;

	public SpriteRenderer splash;

	public SpriteRenderer loading;

	public SpriteRenderer loadingBG;

	private Sprite splashSprite;

	private Dictionary<string, float> loadingTimes;

	private void Awake()
	{
		//Application.targetFrameRate = Globals.targetFramerate;
		QualitySettings.vSyncCount = 0;
		AdManager1.instance.ShowBanner();
	}

	private void Start()
	{
		Globals.appState = AppState.init;
		splashSprite = Resources.Load<Sprite>("Splash");
		splash.sprite = splashSprite;
		splashGreyLeft.color = new Color(splashGreyLeft.color.r, splashGreyLeft.color.g, splashGreyLeft.color.b, 1f);
		splashGreyRight.color = new Color(splashGreyRight.color.r, splashGreyRight.color.g, splashGreyRight.color.b, 1f);
		StartCoroutine(Load());
	}

	private IEnumerator Load()
	{
		Globals.initialResourceLoading = true;
		loadingTimes = new Dictionary<string, float>();
		DOTween.Init(false, true, LogBehaviour.Default);
		DOTween.SetTweensCapacity(50, 50);
		CustomAnalytics.Init();
		SetLoading(0.02f);
		yield return null;
		float startLoad = Time.realtimeSinceStartup;
		string toLoad = "skybox";
		Material skyboxMaterial = Resources.Load<Material>("Materials/_Skybox/Skybox 2");
		if (skyboxMaterial != null)
		{
			RenderSettings.skybox = skyboxMaterial;
		}
		loadingTimes[toLoad] = Time.realtimeSinceStartup - startLoad;
		SetLoading(0.1f);
		yield return null;
		startLoad = Time.realtimeSinceStartup;
		toLoad = "audio";
		GameObject audioRes = Resources.Load<GameObject>("Audio");
		if (audioRes != null)
		{
			GameObject audioGO = Object.Instantiate(audioRes) as GameObject;
			audioGO.name = "Audio";
		}
		loadingTimes[toLoad] = Time.realtimeSinceStartup - startLoad;
		SetLoading(0.2f);
		yield return null;
		startLoad = Time.realtimeSinceStartup;
		toLoad = "Economy";
		GameObject economyRes = Resources.Load<GameObject>("Economy");
		if (economyRes != null)
		{
			GameObject economyGO = Object.Instantiate(economyRes) as GameObject;
			economyGO.name = "Economy";
		}
		loadingTimes[toLoad] = Time.realtimeSinceStartup - startLoad;
		SetLoading(0.3f);
		yield return null;
		startLoad = Time.realtimeSinceStartup;
		toLoad = "sprite manager";
		GameObject smRes = Resources.Load<GameObject>("SpriteManager");
		if (smRes != null)
		{
			GameObject smGO = Object.Instantiate(smRes) as GameObject;
			smGO.name = "SpriteManager";
		}
		loadingTimes[toLoad] = Time.realtimeSinceStartup - startLoad;
		yield return null;
		SetLoading(0.4f);
		startLoad = Time.realtimeSinceStartup;
		toLoad = "menu";
		GameObject menuRes = Resources.Load<GameObject>("UI");
		if (menuRes != null)
		{
			GameObject menuGO = Object.Instantiate(menuRes) as GameObject;
			menuGO.name = "UI";
		}
		yield return null;
		yield return null;
		UserInterface.GetInstance().Init();
		loadingTimes[toLoad] = Time.realtimeSinceStartup - startLoad;
		yield return null;
		SetLoading(0.7f);
		startLoad = Time.realtimeSinceStartup;
		toLoad = "_game_";
		GameObject gameRes = Resources.Load<GameObject>("_GAME_");
		if (gameRes != null)
		{
			GameObject gameGO = Object.Instantiate(gameRes) as GameObject;
			gameGO.name = "_GAME_";
			yield return null;
			yield return null;
			BallBehaviour.GetInstance().StopMovement();
			BallBehaviour.GetInstance().SetToNeutralPosition();
			OrbitCamera.GetInstance().Enable(false);
		}
		loadingTimes[toLoad] = Time.realtimeSinceStartup - startLoad;
		SetLoading(0.8f);
		yield return null;
		startLoad = Time.realtimeSinceStartup;
		toLoad = "screenshot";
		GameObject screenshotRes = Resources.Load<GameObject>("ScreenshotUI");
		if (screenshotRes != null)
		{
			GameObject screenshotGO = Object.Instantiate(screenshotRes) as GameObject;
			screenshotGO.name = "ScreenshotUI";
		}
		yield return null;
		loadingTimes[toLoad] = Time.realtimeSinceStartup - startLoad;
		yield return null;
		SetLoading(0.9f);
		yield return null;
		loading.gameObject.SetActive(false);
		loadingBG.gameObject.SetActive(false);
		Audio.StartMusic(MusicMode.menu);
		SetLoading(1f);
		yield return null;
		float alpha = 1f;
		float speed = 3f;
		while (alpha > 0f)
		{
			splash.color = new Color(splash.color.r, splash.color.g, splash.color.b, alpha);
			splashGreyLeft.color = new Color(splashGreyLeft.color.r, splashGreyLeft.color.g, splashGreyLeft.color.b, alpha);
			splashGreyRight.color = new Color(splashGreyRight.color.r, splashGreyRight.color.g, splashGreyRight.color.b, alpha);
			alpha -= Time.deltaTime * speed;
			yield return null;
		}
		splash.color = new Color(splash.color.r, splash.color.g, splash.color.b, 0f);
		splashGreyLeft.color = new Color(splashGreyLeft.color.r, splashGreyLeft.color.g, splashGreyLeft.color.b, 0f);
		splashGreyRight.color = new Color(splashGreyRight.color.r, splashGreyRight.color.g, splashGreyRight.color.b, 0f);
		yield return null;
		splash.sprite = null;
		Object.DestroyImmediate(splash.gameObject);
		yield return null;
		Resources.UnloadAsset(splashSprite);
		splashSprite = null;
		Object.Destroy(base.gameObject);
		string text = string.Empty;
		foreach (string key in loadingTimes.Keys)
		{
			string text2 = text;
			text = text2 + key + " : loadtime = " + loadingTimes[key].ToString("f5") + "\n";
		}
		Debug.Log(text);
		Globals.appState = AppState.menu;
		UserInterface.GetInstance().DoAfterLoadingFinished();
		Globals.initialResourceLoading = false;
		Resources.UnloadUnusedAssets();
	}

	private void SetLoading(float value)
	{
		Vector3 localScale = loading.transform.localScale;
		Vector3 position = loading.transform.position;
		localScale.x = value * 200f;
		position.x = Mathf.Lerp(-25f / 96f, 0f, value);
		loading.transform.position = position;
		loading.transform.localScale = localScale;
	}
}
